import { Flex, Radio, RadioGroupField } from '@aws-amplify/ui-react'; import { RadioGroupFieldDemo } from './demo'; import { Example, ExampleCode } from '@/components/Example'; import { Fragment } from '@/components/Fragment'; import { ComponentStyleDisplay } from '@/components/ComponentStyleDisplay'; import ThemeExample from '@/components/ThemeExample.mdx'; import { BasicExample, ControlledExample, DirectionExample, DisabledExample, ErrorExample, LabelPositionExample, ReadOnlyExample, SizeExample, RadioGroupFieldThemeExample, } from './examples'; ## Demo ## Usage Import the Radio and RadioGroupField components, and nest the Radio options inside the RadioGroupField. RadioGroupField requires both `label` and `name` for accessibility and form submission, respectively. Radio only requires the `value` prop, which will be submitted with form data. You also have the option to set a Radio as pre-selected by passing its `value` to the `defaultValue` prop on the RadioGroupField. ```jsx file=./examples/BasicExample.tsx ``` ### Controlled component To manually control the RadioGroupField state, you can use the `value` and `onChange` props. ```jsx file=./examples/ControlledExample.tsx ``` ### Sizes Use the `size` prop to change the RadioGroupField size. Available options are `small`, `large`, and none (default). ```jsx file=./examples/SizeExample.tsx ``` ### Direction Use the `direction` prop to change the RadioGroupField direction. Available options are `row` and `column` (default). ```jsx file=./examples/DirectionExample.tsx ``` ### Label position Use the `labelPosition` prop to control where the label is in relation to the Radio. You may pass `labelPosition` to either the RadioGroupField or individual Radios. ```jsx file=./examples/LabelPositionExample.tsx ``` ### State The available RadioGroupField states include `isDisabled` and `isReadOnly`. A disabled field will be not be focusable not mutable and will not be submitted with form data. A readonly field cannot be edited by the user, but will be submitted with form data. #### Disabled radios and radio group ```jsx file=./examples/DisabledExample.tsx ``` #### Read-only radio group ```jsx file=./examples/ReadOnlyExample.tsx ``` ### Validation error Use the `hasError` and `errorMessage` props to mark a RadioGroupField as having a validation error. ```jsx file=./examples/ErrorExample.tsx ``` ### Accessibility / Label behavior {() => import('./../shared/formFieldAccessibility.mdx')} ## CSS Styling ```tsx file=./examples/RadioGroupFieldThemeExample.tsx ``` ### Target classes ### Global styling To override styling on all Radio buttons, you can set the Amplify CSS variables or use the built-in `.amplify-radio__button` class. A B C ```css /* styles.css */ [data-amplify-theme] { --amplify-components-radio-button-background-color: green; --amplify-components-radio-button-border-color: lightgreen; } /* OR */ .amplify-radio__button { background-color: green; border-color: lightgreen; } ``` To replace Radio button styling, unset it: ```css .amplify-radio__button { all: unset; /* Add your styling here*/ } ``` ### Local styling To override styling on a specific Radio, you can use (in order of increasing specificity): a class selector, data attributes, or style props. _Using a class selector:_ Red Orange Yellow ```css /* styles.css */ .custom-radio .amplify-radio__input:checked + .amplify-radio__button { background-color: orange; border-color: red; color: yellow; } ``` ```jsx import { Radio, RadioGroupField } from '@aws-amplify/ui-react'; html ; ``` _Using data attributes:_ ```css /* styles.css */ /* Override only large size styles */ .amplify-radiogroupfield[data-size='large'] { font-size: 1.25rem; } ```